package name.matthewgreet.example5.view; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import name.matthewgreet.example5.dto.GameRatingDTO; import name.matthewgreet.example5.util.FormatLibrary; public class GameRatingListDisplay { private List<GameRatingDisplay> gameRatings; private String totalRatings; public GameRatingListDisplay(Collection<GameRatingDTO> gameRatings) throws Exception { Map<Integer,Integer> ratingCounts; Integer count, rating; int highestCount, totalCount; ratingCounts = new HashMap<>(); for (int i = 0; i <= 10; i++) { ratingCounts.put(i, 0); } this.gameRatings = new ArrayList<>(); totalCount = 0; for (GameRatingDTO gameRating: gameRatings) { rating = gameRating.getRating().intValue(); count = ratingCounts.get(rating); if (count == null) { count = 0; } count = count + gameRating.getUserCount(); totalCount = totalCount + gameRating.getUserCount(); ratingCounts.put(rating, count); } highestCount = 0; for (Entry<Integer,Integer> ratingCount: ratingCounts.entrySet()) { if (ratingCount.getValue() > highestCount) { highestCount = ratingCount.getValue(); } } for (Entry<Integer,Integer> ratingCount: ratingCounts.entrySet()) { this.gameRatings.add(new GameRatingDisplay(ratingCount.getKey(), ratingCount.getValue(), highestCount)); } Collections.sort(this.gameRatings, (grd1,grd2) -> grd2.getRatingValue() - grd1.getRatingValue()); totalRatings = FormatLibrary.formatNumber(totalCount); } public List<GameRatingDisplay> getGameRatings() { return gameRatings; } public String getTotalRatings() { return totalRatings; } }